package com.example.ecommercemvcconfig.advice;


import com.example.ecommercecommon.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;


//全局异常捕获处理
@RestControllerAdvice("com.example")
@Slf4j
public class GlobalExceptionAdvice {


    @ExceptionHandler(value = Exception.class)
    public CommonResponse<String> handlerCommerceException(
            HttpServletRequest req,
            Exception ex
    ) {
        CommonResponse<String> response = new CommonResponse<>(
                -1,"business error"
        );
        response.setData(ex.getMessage());
        log.error("runtime exception: [{}]", ex.getMessage(), ex);
        return response;
    }

}